package com.pig4cloud.pigx.wallet.configuration;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class RabbitMQConfiguration {

	@Bean
	public MessageConverter messageConverter() {
		return new Jackson2JsonMessageConverter();
	}

	@Bean
	public RabbitTemplate.ConfirmCallback confirmCallback(){
		return (correlationData, ack, cause) -> {
			log.info("【发送端确认】- 入参; correlationData: ${}$, ack: ${}$, cause: ${}$", correlationData, ack, cause);
		};
	}

	@Bean
	public RabbitTemplate.ReturnsCallback returnsCallback(){
		return (returned)->log.info("【消息返回】- 入参; returned: ${}$", returned);
	}

	@Bean
	public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
		RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
		rabbitTemplate.setMessageConverter(messageConverter());
		rabbitTemplate.setConfirmCallback(confirmCallback());
		rabbitTemplate.setReturnsCallback(returnsCallback());
		return rabbitTemplate;
	}

}
